package pwd.allen.tag;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;
import pwd.allen.entity.Fruit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 门那粒沙
 * @create 2020-08-08 21:32
 **/
public class PersonBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return Fruit.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String name = element.getAttribute("name");
        Float price = Float.parseFloat(element.getAttribute("price"));
        Date createAt = null;
        try {
            createAt = new SimpleDateFormat("yyyy-MM-dd").parse(element.getAttribute("createAt"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        builder.addPropertyValue("name", name);
        builder.addPropertyValue("price", price);
        builder.addPropertyValue("createAt", createAt);
    }
}
